package org.itboys.mobile.service.mysql.mobile;


import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.TianJieGroupRecordMapper;
import org.itboys.mobile.dto.TianJieGroupRecordDto;
import org.itboys.mobile.entity.mongo.TianJieMember;
import org.itboys.mobile.entity.mysql.TianJieGroup;
import org.itboys.mobile.entity.mysql.TianJieGroupRecord;
import org.itboys.mobile.service.mongo.mobile.TianJieMobileMemberService;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：天街_金融_组织_service
 */
@Service
public class TianJieMobileGroupService extends BaseMySqlService<TianJieGroup> {
    @Autowired
    private TianJieMobileMemberService memberService;
    @Autowired
    private TianJieGroupRecordMapper groupRecordMapper ;
    /**
     * 获取团队钱包。是否
     * @param memberId
     * @return
     */
    @Transactional
    public Map<String,Object> getWallet(Long memberId){
        Map<String,Object> result = new HashMap<String, Object>();
        BigDecimal totalAmount= new BigDecimal("0.00");
        BigDecimal ableAmount = new BigDecimal("0.00");
        result.put("info","字段说明：totalAmount：总拨付金额（暂不使用），ableAmount：可用拨付金额");
        TianJieMember member =memberService.getById(memberId);
        if(member.getFinanceIsCaptain()== SundayCommonConstants.YES){
            TianJieGroup group = super.find(member.getFinanceGroupId());
            ableAmount=group.getPresentAmount();
        }
        result.put("totalAmount",totalAmount);
        result.put("ableAmount",ableAmount);
        return result;
    }

    /**
     * 查询拨付记录
     * @param request
     * @param memberId
     * @return
     */
    public List<TianJieGroupRecordDto> getRecord(HttpServletRequest request,Long memberId){
        List<TianJieGroupRecordDto> dtos = new ArrayList<>();
        TianJieMember member =memberService.getById(memberId);
        //只有队长能查询本队的拨付记录
        if(member.getFinanceIsCaptain()== SundayCommonConstants.YES){
            Map<String,Object> param = ParamUtil.packageMysqlPageParam(request,"");
            dtos =packageRecordDtoMuilt(groupRecordMapper.select(param));
        }
        return  dtos;
    }
    /**
     * 组装交易记录DTO
     * @return
     */
    public List<TianJieGroupRecordDto> packageRecordDtoMuilt(List<TianJieGroupRecord> records){
        List<TianJieGroupRecordDto> dtos = new ArrayList<>();
        for(TianJieGroupRecord record:records){
            dtos.add(new TianJieGroupRecordDto(record.getId(), record.getCreateTime(),record.getCreateDate(),
                    record.getMemberName(),record.getPresentAmount(),record.getType()));
        }
        return dtos;
    }

}
